package my.jwds.p393;

public class ValidUtf8 {
    public static void main(String[] args) {
        ValidUtf8 obj = new ValidUtf8();

        System.out.println(obj.validUtf8(new int[]{250,145,145,145,145}));
    }


    public boolean validUtf8(int[] data) {
        int size = 0;
        for (int i = 0; i < data.length; i++) {
            int temp = getSize(data[i]);
            if (size == 0){
                if (temp == 1 || temp > 4)return false;
                size = temp==0?0:temp-1;
            }else{
                if (temp == 1)size --;
                else return false;
            }
        }
        return size == 0;
    }


    int getSize(int num){
        if (num < 128)return 0;
        int res = 0;
        while(num != 0){
            if (num % 2 ==0){
                res = 0;
            }else{
                res ++;
            }
            num/=2;
        }
        return res ;
    }
}